/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#ifndef BACKENDMANAGER_H
#define BACKENDMANAGER_H

#include <QStringList>

#include <backend.h>

#include <backendinterface.h>

/**
 * Manages backends.
 * Loads static and dynamic backends. Allows for a specific backend to be used
 * for saving. Also, provides access the specified backend. Backends are
 * referenced by name. Conflicts are handled by only loading the first backend
 * with a given name.
 */
class BackendManager
{
    public:
        /**
         * Loads all of the backends. Sets the current backend to the first
         * static backend found.
         */
        BackendManager();
        /**
         * Warning all backends will be deleted when destructor is called.
         * Be sure you are not using references of backends after this is
         * called.
         */
        ~BackendManager();

        /**
         * Gets a list of backends that can be used for opening and saving.
         * @return All of the backends.
         */
        QHash<QString, Backend *> getAvaliableBackends() const;
        /**
         * Gets a pointer to the specified backend.
         * @return A pointer to the backend. If the backend is invalid,
         * return is 0.
         */
        Backend* getCurrentBackend();
        /**
         * Set the current backend by name.
         * Use getCurrentBackend to retrieve the set backend.
         * @return True if the backend was successfully set.
         * @see getCurrentBackend
         */
        bool setCurrentBackend(QString name);

        bool isBackendRemote(QString name);
        QString getBackendDescription(QString name);
        QString getBackendVersion(QString name);

        /**
         * Check if there is a valid backend with the given name.
         * @param name The name of the backend.
         * @return True if it is a valid backend.
         */
        bool isBackend(QString name);

    private:
        void loadStaticBackends();
        void findDynamicBackends();

        /**
         * A mapping of all backends and thier name.
         */
        QHash<QString, Backend *> m_backends;

        /**
         * The backend currently in use.
         */
        Backend *m_backend;
};

#endif /* BACKENDMANAGER_H */
